package com.alexander.scanregisterarouter;

import org.gradle.internal.impldep.org.apache.commons.io.output.ByteArrayOutputStream;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * author : cjw
 * date : 2022/2/9
 * description :
 */
public class Util {
    /**
     * 获取路径下的所有文件/文件夹
     *
     * @param directoryPath  需要遍历的文件夹路径
     * @param isAddDirectory 是否将子文件夹的路径也添加到list集合中
     * @return
     */
    public static List<File> getAllFile(String directoryPath, boolean isAddDirectory) {
        List<File> list = new ArrayList<>();
        File baseFile = new File(directoryPath);
        if (baseFile.isFile() || !baseFile.exists()) {
            return list;
        }
        File[] files = baseFile.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    if (isAddDirectory) {
                        list.add(file);
                    }
                    list.addAll(getAllFile(file.getAbsolutePath(), isAddDirectory));
                } else {
                    list.add(file);
                }
            }
        }
        return list;
    }

//    public static byte[] toByteArray(InputStream input) throws IOException {
//        ByteArrayOutputStream output = new ByteArrayOutputStream();
//        Throwable var2 = null;
//
//        byte[] var3;
//        try {
//            copy((InputStream)input, (OutputStream)output);
//            var3 = output.toByteArray();
//        } catch (Throwable var12) {
//            var2 = var12;
//            throw var12;
//        } finally {
//            if (output != null) {
//                if (var2 != null) {
//                    try {
//                        output.close();
//                    } catch (Throwable var11) {
//                        var2.addSuppressed(var11);
//                    }
//                } else {
//                    output.close();
//                }
//            }
//
//        }
//
//        return var3;
//    }
//
//    private static int copy(InputStream input, OutputStream output) throws IOException {
//        long count = copyLarge(input, output);
//        return count > 2147483647L ? -1 : (int)count;
//    }
//
//    private static long copy(InputStream input, OutputStream output, int bufferSize) throws IOException {
//        return copyLarge(input, output, new byte[bufferSize]);
//    }
//
//    private static long copyLarge(InputStream input, OutputStream output) throws IOException {
//        return copy(input, output, 4096);
//    }
//
//    private static long copyLarge(InputStream input, OutputStream output, byte[] buffer) throws IOException {
//        long count;
//        int n;
//        for(count = 0L; -1 != (n = input.read(buffer)); count += (long)n) {
//            output.write(buffer, 0, n);
//        }
//
//        return count;
//    }
}
